﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CCH.DAO;
using CCH.Entity;
using CCH.Lib;

public partial class _10_1001_1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack) 
        {
            sysfunctionDAO dao = new sysfunctionDAO();

            if (Request.QueryString["mode"] == "new")
            {
                this.lab_title.Text = dao.Get_titleName(1001) + " - 新增";
                
                
            }
            else
            {
                this.lab_title.Text = dao.Get_titleName(1001) + " - 修改";
                this.hidd_no.Value = Request.QueryString["ID"];
                

                doctorprofile data = new doctorprofileDAO().Get_doctorprofile(int.Parse(this.hidd_no.Value));

                this.tbox_name.Text = data.doc_name;
                this.tbox_ename.Text = data.doc_ename;
                this.tbox_title.Text = data.doc_title;
                this.tbox_job.Text = data.doc_job;
                this.tbox_exp.Text = data.doc_experience.ConvertToNewLine();
                this.tbox_skill.Text = data.doc_skill.ConvertToNewLine();
                this.tbox_order.Text = data.doc_order.Value.ToString();

                if (!string.IsNullOrEmpty(data.doc_photopath))
                {
                    this.FileUpload1.doc_PICpath = data.doc_photopath + data.doc_photoname;
                }
            }

        }
        
    }

    protected void Page_LoadComplete(object sender, EventArgs e)
    {
        this.tbox_name.Focus();
    }
    
    protected void btn_ok_Click(object sender, EventArgs e)
    {
        if (this.check_Input())
        {
            string str = "";
            doctorprofile data;
            doctorprofileDAO dao = new doctorprofileDAO();

            if (this.hidd_no.Value != "")
            {
                data = dao.Get_doctorprofile(int.Parse(this.hidd_no.Value));
                str = "修改完成!";
            }
            else
            {
                data = new doctorprofile();
                str = "新增完成!";
                data.state = "1";
            }

            data.doc_name = this.tbox_name.Text;
            data.doc_ename = this.tbox_ename.Text;
            data.doc_title = this.tbox_title.Text;
            data.doc_job = this.tbox_job.Text;
            data.doc_experience = this.tbox_exp.Text.ConvertToBR();
            data.doc_skill = this.tbox_skill.Text.ConvertToBR();
            data.doc_order = int.Parse(this.tbox_order.Text);
            data.createuid = new SessionObject().SessionUserID;
            data.createtime = DateTime.Now;


            string filename = this.FileUpload1.Get_FileName;
            string filepath = this.FileUpload1.Get_FilePath;

            if (!string.IsNullOrEmpty(filename))
            {
                data.doc_photoname = filename;
                data.doc_photopath = filepath;
                data.doc_phototype = filename.Split('.')[1];
            }

            if (this.hidd_no.Value == "")
            {
                dao.Add_doctorprofile(data);
            }

            //更新
            dao.Update();

            //更新父代網頁
            JsUtil.UpdateParentJs(this, str);
        }
    }

    private bool check_Input()
    {
        if (string.IsNullOrEmpty(this.tbox_name.Text))
        {
            JsUtil.AlertJs(this, "請輸入醫師姓名!!");
            return false;
        }

        if (string.IsNullOrEmpty(this.tbox_job.Text))
        {
            JsUtil.AlertJs(this, "請輸入醫師現任!!");
            return false;
        }

        if (!this.tbox_order.Text.IsNumber())
        {
            JsUtil.AlertJs(this, "排序請輸入數字!!");
            return false;
        }

        return true;
    }
}